Variables – PHP
Variables are used to store data and can be called by using the variable name and the data that it holds will be outputted instead.
For example, I want to save the message “Hello Hirdesh” in memory and then first display hello Hirdesh and afterward Welcome to PHP World, I will write the following simple programme
// Example of concatenate string.
<?php
$text = “Hello Hirdesh”;
echo $text.”Welcome to PHP World”;
?>
Please Note that dot (.) operator is used to concatenate or combine string in php
Naming Conventions
- Variable name start with the dollar sign
- Must contain only letters, numbers, and the underscore sign
- Cannot start with a numeric value
- Variable name are case sensitive
To minimize confusion and errors, it’s best to stick to one naming scheme. The two most common styles are
Use all lowercase, separating words with underscores: $first_name, $emp_code, etc.
Use primarily lowercase, separating words with capital letters: $textName, $fName, etc.
Assigning Values
Variables can be assigned using the assignment operator (=). Some examples are below-
$status = false;
$empCode = 231;
$average = 86.67;
$name = “Jyoti”;
$con = mysqli_connect(‘localhost’, root’, ”, ‘mydb’);
$key = NULL;
Object variables are assigned using the assignment operator with the new keyword:
$ob = new myClass();
Example – calculate sum of 2 numbers using variables
<?php
// program to calculate sum of two numbers
$num1 =20;
$num2=30;
$sum=$num1+$num2;
echo “Sum is ”.$sum;
?>
Example – define function
<?php
define(“tutorial”,”Programing Logic and Technique”);
echo constant(“tutorial”);
?>
Output : Programming Logic and Technique
To Buy complete book kindly download – php mysql for advanced learning from amazon, flipkart, google playstore and many more channels.